//
//  CorgiStatusRetweetView.swift
//  weibo
//
//  Created by Corgi on 17/3/24.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit
import SnapKit

class CorgiStatusRetweetView: UIView {

    private var statusViewBottomConstraint: Constraint?
    
    var statusViewModel: CorgiStatusViewModel? {
        
        didSet {
            
            contentLabel.attributedText = statusViewModel?.retweetAttributedString
            
            statusViewBottomConstraint?.deactivate()
            
            if let picUrls = statusViewModel?.homeModel?.retweeted_status?.pic_urls, picUrls.count > 0 {
                
                pictureView.picUrls = picUrls
                
                self.snp.makeConstraints({ (make) in
                    statusViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HOMECELLMARGIN).constraint
                })
                
                pictureView.isHidden = false
                
            } else {
                
                self.snp.makeConstraints({ (make) in
                    statusViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HOMECELLMARGIN).constraint
                })
                
                pictureView.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //backgroundColor = randomColor()
        backgroundColor = UIColor(white: 235 / 255, alpha: 1)
        
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        contentLabel.snp.makeConstraints { (make) in
            make.leading.top.equalTo(HOMECELLMARGIN)
            make.width.equalTo(screenWidth - 2 * HOMECELLMARGIN)
        }
        contentLabel.textAlignment = .left
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(HOMECELLMARGIN)
            make.top.equalTo(contentLabel.snp.bottom).offset(HOMECELLMARGIN)
        }
        
        self.snp.makeConstraints { (make) in
            statusViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HOMECELLMARGIN).constraint
        }
    }
    
    private lazy var contentLabel: UILabel = UILabel(title: "转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博", textColor: UIColor.darkGray, fontSize: 14)
    
    private lazy var pictureView: CorgiStatusPictureView = CorgiStatusPictureView()
}
